﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Xml.Serialization;
using System.Text;
using Core;
using PublicService.Properties;
using System.IO;

namespace PublicService
{
    /// <summary>
    /// Сервис для публикации рейтингов
    /// </summary>
    [WebService(Namespace = "http://jcorp.ru/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Publish : System.Web.Services.WebService
    {
        [WebMethod]
        public string Public(string xml)
        {
            try
            {
                SoapHexBinary formatter = new SoapHexBinary();
                string data = Encoding.UTF8.GetString(SoapHexBinary.Parse(xml).Value);
                SchoolClass cl = RetingManagement.ReadDataXML(data);
                string path = this.Server.MapPath("App_Data");
                int counter = Convert.ToInt32(File.ReadAllText(path + "\\counter.jc"));
                counter++;
                File.WriteAllText(path + "\\counter.jc", counter.ToString());
                RetingManagement.SaveData(
                    this.Server.MapPath("Reports") + "\\report_" + counter.ToString() + ".xml", cl);

                RetingManagement.RenderWebPage(
                    this.Server.MapPath("Reports") + "\\report_" + counter.ToString() + ".htm", cl);

                return PublicService.Properties.Settings.Default.PublicSite;
            }
            catch
            {
                return "Ошибка обработки данных";
            }
        }
    }
}